package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.appDynamic.entity.resp.UserInfoResp;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsStudentLoanProcess;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsStudentLoanProcessReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.resp.SmsWnsStudentLoanProcessResp;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.mapper.SmsWnsStudentLoanProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsStudentLoanProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.constant.SchoolFeesDelayObjectTypeConstant.SCHOOL_FEES_DELAY_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.constant.SchoolFeesDelayObjectTypeConstant.SCHOOL_FEES_DELAY_OBJECT_TYPE_ROLE;

/**
 * <p>
 * 助学贷款审批流程 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-03-20
 */
@Service
public class SmsWnsStudentLoanProcessServiceImpl extends ServiceImpl<SmsWnsStudentLoanProcessMapper, SmsWnsStudentLoanProcess> implements SmsWnsStudentLoanProcessService {

    @Resource
    private CacheUtil cacheUtil;
    @Resource
    private SmsRoleMapper smsRoleMapper;

    @Override
    public ResponseResult updateStudentLoanProcess(SmsWnsStudentLoanProcessReq req) {
        if (isNullOrEmpty(req.getId(), req.getProcessObjectType(), req.getProcessIsApplicationEdit(),
                req.getProcessIsUse(), req.getProcessIsStamp(), req.getProcessIsSign())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        SmsWnsStudentLoanProcess process = this.getById(req.getId());
        if (process == null) {
            return CommonResult.failed(CommonCodeEnum.SCHOOL_FEES_DELAY_PROCESS_NOT_EXIST);
        }

        if (!isNullOrEmpty(req.getProcessObjectId())) {
            if (req.getProcessObjectType() == SCHOOL_FEES_DELAY_OBJECT_TYPE_PERSON && !req.getProcessIsApplicationEdit()) {
                SmsUser smsUser = cacheUtil.getUserInfo(req.getProcessObjectId());
                if (smsUser == null || isNullOrEmpty(smsUser.getId())) {
                    return CommonResult.failed(CommonCodeEnum.SCHOOL_FEES_DELAY_PROCESS_USER_NOT_EXIST);
                }
            } else if (req.getProcessObjectType() == SCHOOL_FEES_DELAY_OBJECT_TYPE_ROLE) {
                SmsRole smsRole = smsRoleMapper.selectById(req.getProcessObjectId());
                if (smsRole == null || isNullOrEmpty(smsRole.getId())) {
                    return CommonResult.failed(CommonCodeEnum.SCHOOL_FEES_DELAY_PROCESS_ROLE_NOT_EXIST);
                }
            }
        }

        SmsWnsStudentLoanProcess updateProcess = new SmsWnsStudentLoanProcess();
        BeanUtils.copyProperties(req, updateProcess, "processSort");

        this.updateById(updateProcess);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getStudentLoanProcessList() {
        List<SmsWnsStudentLoanProcessResp> collect = this.list(new LambdaQueryWrapper<SmsWnsStudentLoanProcess>()
                .orderByAsc(SmsWnsStudentLoanProcess::getProcessSort)).stream().map(process -> {
            SmsWnsStudentLoanProcessResp processResp = new SmsWnsStudentLoanProcessResp();
            BeanUtils.copyProperties(process, processResp);

            if (process.getProcessObjectType() != null && !isNullOrEmpty(process.getProcessObjectId())) {
                if (process.getProcessObjectType() == SCHOOL_FEES_DELAY_OBJECT_TYPE_PERSON) {
                    SmsUser smsUser = cacheUtil.getUserInfo(process.getProcessObjectId());
                    if (smsUser != null && !isNullOrEmpty(smsUser.getId())) {
                        UserInfoResp userResp = new UserInfoResp();
                        BeanUtils.copyProperties(smsUser, userResp);
                        processResp.setProcessObjectName(smsUser.getRealName());
                        processResp.setUserInfo(userResp);
                    }
                } else {
                    SmsRole smsRole = smsRoleMapper.selectById(process.getProcessObjectId());
                    if (smsRole != null && !isNullOrEmpty(smsRole.getId())) {
                        processResp.setProcessObjectName(smsRole.getRoleName());
                    }
                }
            }
            return processResp;
        }).collect(Collectors.toList());
        return CommonResult.success(collect);
    }
}
